package com.peanut.baby.mvp.expert;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.User;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/22.
 */

public class ExpertProfilePresenter implements ExpertProfileContract.Presenter{

    private ExpertProfileContract.View view;
    private Activity                    activity;
    public ExpertProfilePresenter(ExpertProfileContract.View view,Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getDoctorInfo(String docId) {
        Observable obs = RetrofitClient.getInstance().getDoctorInfo(docId,InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<User>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<User>() {
            @Override
            protected void onHandleSuccess(User o) {
                view.onDoctorInfoGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetDoctorInfoFailed(msg);
            }
        });
    }

    @Override
    public void changeFollowStatus(String targetId) {
        Observable obs = RetrofitClient.getInstance().changeFollowStatus(InitManager.getInstance().getUserId(),targetId,
                InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<User>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<User>() {
            @Override
            protected void onHandleSuccess(User o) {
                view.onChangeStatusSuccess(o.isConcerned);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onChangeStatusFailed(msg);
            }
        });
    }
}
